package com.example.visitedsys.converter;

import com.example.visitedsys.informationGet.UserVO;
import com.example.visitedsys.mapper.PartMapper;
import com.example.visitedsys.pojo.Part;
import com.example.visitedsys.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  SongPeng
 * @date  2021/10/19 14:22
 * @version 1.0
 */

@Component
public class UserConverter {

    @Autowired
    private PartMapper departmentMapper;

    /**
     * 转voList
     * @param users
     * @return
     */
    public  List<UserVO> converterToUserVOList(List<User> users){
        List<UserVO> userVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(users)){
            for (User user : users) {
                UserVO userVO = converterToUserVO(user);
                userVOS.add(userVO);
            }
        }
        return userVOS;
    }

    /**
     * 转vo
     * @return
     */
    public  UserVO converterToUserVO(User user){
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        userVO.setUserStatus(user.getUserStatus() == 0);
        Part department = departmentMapper.selectByPrimaryKey(user.getPartId());
        if(department!=null&&department.getPartName()!=null){
            userVO.setPartName(department.getPartName());
            userVO.setPartId(department.getPartId());
        }

        return userVO;
    }


}
